package com.tencreat.mq;

import cn.hutool.extra.cglib.CglibUtil;
import com.rabbitmq.client.Channel;
import com.tencreat.common.core.domain.dto.AfterSaleDTO;
import com.tencreat.common.enums.AfterSaleStatus;
import com.tencreat.domain.mall.MallAftersale;
import com.tencreat.domain.mall.dto.AftersaleAuditDTO;
import com.tencreat.service.mall.IMallAftersaleService;
import lombok.extern.slf4j.Slf4j;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.amqp.core.Message;
import org.springframework.amqp.rabbit.annotation.RabbitHandler;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.io.ByteArrayInputStream;
import java.io.ObjectInputStream;
import java.util.Objects;

/**
 * 取消订单消息的处理者
 * Created by macro on 2018/9/14.
 */
@Component
@RabbitListener(queues = "base-mall.after.sale.confirm.name")
@Slf4j
public class ConfirmAfterSaleConsumer {
    private static Logger LOGGER =LoggerFactory.getLogger(ConfirmAfterSaleConsumer.class);

    @Autowired
    private IMallAftersaleService mallAftersaleService;

    @RabbitHandler(isDefault=true)
    public void handle(Message message, Channel channel) throws Exception{
        log.info("进入售后自动完成队列--------------------------------------");
        ByteArrayInputStream byteInt = new ByteArrayInputStream(message.getBody());
        ObjectInputStream objInt = new ObjectInputStream(byteInt);
        AfterSaleDTO orderVO = (AfterSaleDTO) objInt.readObject();

        long deliveryTag = message.getMessageProperties().getDeliveryTag();
        //信息为空,丢弃该信息
        if (Objects.isNull(orderVO)) {
            log.info("消息为空,手动丢弃该消息....");
            channel.basicNack(deliveryTag, false, false);
            return;
        }
        MallAftersale aftersale = mallAftersaleService.lambdaQuery()
                .eq(MallAftersale::getOrderGoodsId, orderVO.getOrderGoodsId())
                .eq(MallAftersale::getId, orderVO.getId())
                .one();
        if (aftersale == null){
            channel.basicAck(deliveryTag, false);
        }else {
            LOGGER.info("自动完成售后----:{}",orderVO.getOrderGoodsId());
            log.info("对象的值-----:{}",orderVO.getStatus());
            // 根据类型进行判断
            if (orderVO.getType() == 1 && aftersale.getStatus() != AfterSaleStatus.ALREADY_APPLIED.getValue()){
                LOGGER.info("自动收货确认类型{}，收货状态不是----:{}",orderVO.getType(),AfterSaleStatus.ALREADY_APPLIED.getDesc());
                channel.basicAck(deliveryTag, false);
                return;
            }
            if (orderVO.getType() == 2 && aftersale.getStatus() != AfterSaleStatus.WAIT_RECEIVED.getValue()){
                LOGGER.info("自动收货确认类型{}，收货状态不是----:{}",orderVO.getType(),AfterSaleStatus.WAIT_RECEIVED.getDesc());
                channel.basicAck(deliveryTag, false);
                return;
            }
            AftersaleAuditDTO dto = new AftersaleAuditDTO();
            dto.setOrderGoodsId(orderVO.getOrderGoodsId());
            // 退款退货通过
            if (orderVO.getStatus() == AfterSaleStatus.BUYER_DEAL.getValue()){
                mallAftersaleService.auditMallAftersale(dto);
            }else if (orderVO.getStatus() == AfterSaleStatus.DEL_PAY.getValue()){
                // 去退款
                mallAftersaleService.updateMallAftersale(dto);
            }
            channel.basicAck(deliveryTag, false);
        }
    }
}
